<?php

/**
 * This is the model class for table "article".
 *
 * The followings are the available columns in table 'article':
 * @property integer $id
 * @property string $caption
 * @property string $h1
 * @property string $title
 * @property string $keywords
 * @property string $description
 * @property string $detail_text
 * @property string $anons_text
 * @property string $date_create
 *
 * The followings are the available model relations:
 * @property ArticlePhotos[] $articlePhotoses
 */
class Article extends CActiveRecord
{
	public $image;
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'article';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('caption, detail_text, anons_text, date_create', 'required'),
			array('caption', 'length', 'max'=>256),
			array('anons_text', 'length', 'max'=>512),
			//array('image', 'file', 'types'=>'jpg, gif, png'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, caption, image, detail_text, anons_text, date_create', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'articlePhotoses' => array(self::HAS_MANY, 'ArticlePhotos', 'article_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'caption' => 'Caption',
			'detail_text' => 'Detail Text',
			'anons_text' => 'Anons Text',
			'date_create' => 'Date Create',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('caption',$this->caption,true);
		$criteria->compare('detail_text',$this->detail_text,true);
		$criteria->compare('anons_text',$this->anons_text,true);
		$criteria->compare('date_create',$this->date_create,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Article the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	

	public function afterSave () {
		$img = CUploadedFile::getInstance($this, 'image');
		$v = $img->getName();
		if (empty($v)) return parent::afterSave;
		
		$imageName = md5(microtime()) . '.jpg';
		$path = Article::getFolder($imageName, true).$imageName;
		if ($img->saveAs($path)) {
			$advImg = new ArticlePhotos();
			$advImg->article_id = $this->getPrimaryKey();
			$advImg->path = $imageName;
			$advImg->save();
	
			//if (count($advImg->getErrors()))
			print_r($advImg->getErrors()); //die();
		}
	}
	
	public static function getFolder($imageName, $withWebRoot = false) {
		$folder = $withWebRoot ? Yii::getPathOfAlias('webroot') : '';
		$folder .=  '/uploads/article/'.substr($imageName, 0, 3).'/';
	
		if (is_dir($folder) == false)
			@mkdir($folder, 0755, true);
		return $folder;
	}
}
